package org.grow.pointcolor.widget;

import org.grow.pointcolor.tool.MeasureAtomicBoolean;
import org.grow.pointcolor.tool.ScanTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;

@Component
public class StartButton extends JButton implements MouseListener {
    public StartButton() {
        super("开始");
    }
    @Autowired
    private ExecutorService es;
    @Autowired
    private ScanTask scanTask;
//    表征是否在扫描点和颜色 初始化状态时不扫描 点击开始按钮后扫描
//    private AtomicBoolean running;
    private MeasureAtomicBoolean running;
    @PostConstruct
    public void springInit(){
        setSize(20, 10);
//        running=new AtomicBoolean(false);
        running = new MeasureAtomicBoolean(false);
        addMouseListener(this);

    }


    @Override
    public void mouseClicked(MouseEvent e) {
/*        if (!running.get()) {
//                目前是 开始 按钮
            this.setText("停止");
//                开启任务内循环钥匙
            scanTask.getLoopkey().set(true);
            System.out.println("scan task loop key"+scanTask.getLoopkey());
//                s.set(true);
//                提交任务给线程池
            this.es.submit(scanTask);
//                薛定谔的猫
            running.set(true);

        } else {
//                目前是 停止 按钮
            this.setText("稍等");
//                关闭任务内循环钥匙
            scanTask.getLoopkey().set(false);
// 稍等循环执行完毕 跳出任务内循环后会拉闸 此处需要等待
            try {
                scanTask.getCountDownLatch().await();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            this.setText("开始");
//                薛定谔的猫
            running.set(false);
//
        }*/
//
        if (!running.measureGet()) {
//                目前是 开始 按钮
            this.setText("停止");
//                开启任务内循环钥匙
            scanTask.getLoopkey().set(true);
//            System.out.println("scan task loop key"+scanTask.getLoopkey());
//                s.set(true);
//                提交任务给线程池
            this.es.submit(scanTask);

        } else {
//                目前是 停止 按钮
            this.setText("稍等");
//                关闭任务内循环钥匙
            scanTask.getLoopkey().set(false);
// 稍等循环执行完毕 跳出任务内循环后会拉闸 此处需要等待
            try {
                scanTask.getCountDownLatch().await();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            this.setText("开始");
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

}
